<template>
  <div>
    <h1><router-link to="/">返回首页</router-link></h1>
    <div style="margin-bottom:1rem">
      <label for="">路径：</label>
      <input v-model="dir" @click="chooseDir" style="width: 20rem" readonly />
      <input type="file" style="display: none" ref="file" @change="getFile" />
    </div>
    <textarea
      v-if="readIni"
      v-model="readIni"
      style="height: 50vh; width: 25rem"
    />
    <div style="margin: 1rem">
      <button @click="saveDir">保存</button>
    </div>
  </div>
</template>

<script>
import { dls } from "@/module/local-store";
import {fc} from "@/module/frp-control";
export default {
  data() {
    return {
      dir: "",
      readIni: "",
    };
  },
  methods: {
    saveDir() {
      console.log(this.dir);
      dls.setDir(this.dir);
      this.write()
      location.reload();
    },
    chooseDir() {
      this.$refs.file.click();
    },
    getFile(e) {
      try {
        const path = e.target.files[0].path;
        this.dir = path;
        console.log(path);
      } catch (error) {
        console.log("未选择");
      }
    },
    read() {
      fc.read((err, data) => {
        this.readIni = "";
        if (err) throw err;
        this.readIni += data;
        console.log("read success");
      });
    },
    write() {
      fc.write(this.readIni, (err) => {
        if (err) throw err;
        console.log("write success");
        this.read();
      });
    },
  },
  created() {
    this.dir = dls.getDir();
    this.read()
  },
};
</script>

<style>

</style>